package com.iamteer.day11;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sound.midi.Soundbank;

/**
 * Servlet implementation class Path
 */
@WebServlet("/Path")
public class Path extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public Path() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/**
		 * 目标资源给服务器使用：/ 表示当前 web 应用的根目录
		 * 目标资源给浏览器使用：/ 表示在 webapps 的根目录下
		 * */
		
		//1. 转发路径：服务器处理
//		request.getRequestDispatcher("/index.html").forward(request, response);
		
		//2. 重定向路径
//		response.sendRedirect("/day11/index.html");
		
		//3. html 页面的超链接路径
//		response.getWriter().write("<html><body><a href='/day11/index.html'>Link</a></body></html>");
		
		//4. html 页面中的 form 提交地址
//		response.getWriter().write("<html><body><form action='/day11/index.html'><input type='submit'/></form></body></html>");

		//查看当前的路径
//		System.out.println(new File(".").getAbsolutePath());
		
		//1. getRealPath
//		String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
//		System.out.println(path);
//		File file = new File(path);
//		FileInputStream in = new FileInputStream(file);
		
		//2. getResourceAsStream()
		InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
		
		Properties prop = new Properties();
		prop.load(in);
		
		String user = prop.getProperty("user");
		String password = prop.getProperty("password");
		System.out.println("user=" + user);
		System.out.println("password=" + password);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
